/*
 * Static.h
 *
 *  Created on: 2013. 8. 5.
 *      Author: jjaehuny
 */

#ifndef FRIENDSSERVER_STATIC_H_
#define FRIENDSSERVER_STATIC_H_

#include <map>
#include <vector>
#include <memory>

#include <Toolkit/Single.h>

namespace FriendsServer {

class User;

static const int s_MaxUser = 20000;
static const int s_Mode = 10;

typedef Toolkit::Pool<User> POOL;
typedef std::map<int, std::shared_ptr<User> > MAP;

class Static : public ::Toolkit::Single<Static>
{
public:
	Static() : clsUserPool(s_MaxUser), s_clsTreeVector(s_Mode), is_test(0), rankCycle(0) {}

	std::shared_ptr<User>	GetUser(uint32_t seq)
	{
		int mod = seq % s_Mode;
		MAP & clsMap = Static::Instance().s_clsTreeVector[mod];
		MAP::iterator itr = clsMap.find(seq);
		if (clsMap.end() == itr)
		{
			return NULL;
		}
		return itr->second;
	}

	std::shared_ptr<User>	SetUser(uint32_t seq)
	{
		int mod = seq % s_Mode;
		MAP & clsMap = Static::Instance().s_clsTreeVector[mod];
		std::shared_ptr<User> clsuser = Static::Instance().clsUserPool.Create();
		clsMap[seq] = clsuser;

		return clsuser;
	}

	void	EraseUser(uint32_t seq)
	{
		int mod = seq % s_Mode;
		MAP & clsMap = Static::Instance().s_clsTreeVector[mod];
		MAP::iterator itr = clsMap.find(seq);
		if (clsMap.end() == itr)
		{
			LOG(DEV, "not found user seq : ", seq);
			return;
		}

		LOG(DEV, "erase found user seq : ", seq);
		clsMap.erase(seq);
	}

	std::vector<MAP> & GetTreeVector() { return s_clsTreeVector; }

private:
	POOL clsUserPool;
	std::vector<MAP> s_clsTreeVector;

public:
	int is_test;
	int rankCycle;
};

} /*FriendsServer*/


#endif /* STATIC_H_ */
